package com.cxw.healthserver.controller

import com.cxw.healthserver.constant.Constants
import com.cxw.healthserver.entity.User
import com.cxw.healthserver.resolver.CurrentUser
import com.cxw.healthserver.response.BaseResponse
import com.cxw.healthserver.response.LoginResponse
import com.cxw.healthserver.response.RemotePageDataBean
import com.cxw.healthserver.response.UploadResidentResponse
import com.cxw.healthserver.service.ResidentService
import com.fasterxml.jackson.databind.JsonNode
import org.json.JSONObject
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

/**
 * @createTime 2024/6/22 16:34
 * @author 崔兴旺
 * @description
 */
/**
 * @createTime 2024/6/16 11:41
 * @author 崔兴旺
 * @description
 */
@RestController
@RequestMapping("api")
class ResidentController {

    @Autowired
    private lateinit var residentService: ResidentService

    @GetMapping("downloadResident", produces = [Constants.produces])
    fun downloadResident(@CurrentUser user: User, @RequestParam idNumber: String): BaseResponse<JsonNode> {
        return residentService.findResident(user.username,idNumber)
    }

    @PostMapping("uploadResident", produces = [Constants.produces])
    fun uploadResident(@CurrentUser user: User, @RequestBody jsonStr: String): BaseResponse<Any> {
        println("收到上传居民的json：${jsonStr}")
        //{"addWay":"2","bhcExt":{"areaCode":"","local":"","localData":null,"localDatatype":"","localJsontype":"","localSchema":"","localVersion":"","origBhcExtJson":"","servCrtTime":"","servUptTime":"","srcCrtTime":1719129487880,"srcDoctAccount":"1","srcDoctCertNo":"","srcDoctCerttype":"","srcDoctId":"","srcDoctName":"崔兴旺","srcEid":"AIODM_no-device-id","srcRevision":"2024-06-23 15:58:07#fb5d37588af444d5860602059a762a35","srcUptTime":1719129487880,"srcVendor":"WeDoctor","version":"2.1"},"birthday":"1997-07-28","certType":"1","classifyCode":"103","classifyName":"普通人群","diseaseTag":[{"code":"107","description":"高血压"},{"code":"108","description":"糖尿病"},{"code":"110","description":"肺结核"},{"code":"109","description":"严重精神障碍"}],"gender":2,"certNo":"110101199707288406","mobile":"","name":"张三","nation":"","sourceTableId":"2208f1f7f5a747a2889483da5feeced1"}
        return residentService.saveResident(user.username, jsonStr)
    }

    //http://10.111.0.111:8080/api/getResidentList?pageNum=1&pageSize=8&realName=aaaaaaaaaaaa&classify=104&diseaseTags=107%2C108%2C110%2C109&signState=1&isHealthdoc=true
    @GetMapping("queryResidentList")
    fun queryResidentList(@CurrentUser user: User, @RequestParam pageNum: Long, @RequestParam pageSize: Long, @RequestParam realName: String?,@RequestParam classify: Int?, @RequestParam diseaseTags: String?, @RequestParam signState: Int?, @RequestParam isHealthdoc: Boolean?) : BaseResponse<RemotePageDataBean<JsonNode>> {
        return residentService.queryResidentList(user, pageNum, pageSize,realName, classify, diseaseTags, signState, isHealthdoc)
    }
}